﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OnlineEducation.Data.Entities;
using OnlineEducation.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OnlineEducation.Data;
using Microsoft.AspNetCore.Hosting;
using OnlineEducation.Models.ServerAPIModels;
using OnlineEducation.Helpers;
using OnlineEducation.Constants;
using OnlineEducation.Models.TrailerViewModels;

namespace OnlineEducation.Areas.Trailer.Controllers
{

    [Area("Trailer")]
    [Route("[controller]/[action]")]
  
    public class TrailerController : BaseController
    {

        private readonly ILogger _logger;
        private readonly ApplicationDbContext _context;
        private readonly IWebHostEnvironment _env;

        public TrailerController(ApplicationDbContext context, ILogger<TrailerController> logger, IWebHostEnvironment env)
        {
            _context = context;
            _logger = logger;
            _env = env;
        }
       
        public IActionResult Index()
        {
            var viewModel = new CreateMeeting();
            viewModel.name = "免费体验教室";
            return View(viewModel);
        }

        [HttpGet]
        //[ValidateAntiForgeryToken]
        public ActionResult Join(string meetingId,string name)
        {

            var joinmeeting = new JoinMeeting();
            joinmeeting.fullName = name;
            joinmeeting.meetingID = meetingId;
            joinmeeting.password = "666666";
            joinmeeting.userID = name + "-"+ meetingId;
            joinmeeting.redirect = "true"; // 直接返回了html页面， 在为false的时候，返回xml结果，包含会议地址
            joinmeeting.joinViaHtml5 = "true";
            //joinmeeting.clientURL = "https://elearning.futureinfo.cn/";
            var param = Utility.CreateParams<JoinMeeting>(joinmeeting, ServerAPI.Join);
            return Json(new { success = true, resurl = param });
        }

        public IActionResult TrailerInfo(string name)
        {
            // 新建教室
            var data = new CreateMeeting();
            data.name = "免费体验教室";
            data.attendeePW = "666666";
            data.moderatorPW = "666888";
            data.welcome = "欢迎体验";
            data.bannerText = "百灵鸟在线教室";
            data.duration = 20;
            data.logo = "";
            data.meetingID = Guid.NewGuid().ToString();
            var param = Utility.CreateParams<CreateMeeting>(data, ServerAPI.Create);

            var httprequest = HttpHelper.HttpsGet("http://elearning.futureinfo.cn/bigbluebutton/api/create?" + param);

            var response = Utility.XMLToModel<CreateResponseEntity>(httprequest);
            if(response.returncode != "SUCCESS")
            {
                return Json(new { success = false, message ="体验教室新建失败，请稍候再试" });
            }
            var meetingInfo = new TrailerViewMeeting();
            meetingInfo.MeetingAddress = string.Format("http://{0}/Trailer/Index?name={1}&&meetingId={2}", "onlineeducation.futureinfo.cn", string.Empty, response.meetingID);
            meetingInfo.Name = name;
            meetingInfo.MeetingId = response.meetingID;
            return View(meetingInfo);
            
        }
    }
}
